package nuc.edu.tlias.controller;

import lombok.extern.slf4j.Slf4j;
import nuc.edu.tlias.pojo.PageResult;
import nuc.edu.tlias.pojo.Result;
import nuc.edu.tlias.pojo.Student;
import nuc.edu.tlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {


    @Autowired
    StudentService studentService;



    @GetMapping
    //分页查询

    public Result findall(String name,Integer degree,Integer clazzId,Integer page,Integer pageSize){

        log.info("接收参数：{},{},{},{},{}", name,degree,clazzId,page,pageSize);

        PageResult<Student> pageResult = studentService.findall( name,degree,clazzId,page,pageSize);
        return Result.success(pageResult);
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids){
        log.info("删除学生：{}", ids);
        Integer[] idArray = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toArray(Integer[]::new);
        studentService.delete(idArray);
        return Result.success();
    }



    @PostMapping
    public Result save(@RequestBody Student student){
        studentService.save(student);

        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Student student =  studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result upload(@RequestBody Student student){
        studentService.upload(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("================={},{}",id,score);
        if (id != null){
            studentService.violation(id,score);
        }
        return Result.success();
    }
}
